<?php

require_once 'phpmoko/ui/Widget.php';

class DockPanel extends Widget
{
	protected $spacing = 0;
	private $widgets = array();
	private $positions = array();

	function __construct()
	{
		parent::__construct();

		$this->add(new HTML('&nbsp;'), DockStyle::FILL);

		$this->setStyleName('pmk-DockPanel');
	}

	function add($widget, $position)
	{
		if ($position == DockStyle::FILL)
		{
			$this->widgets[0] = $widget;
			$this->positions[0] = $position;
			return;
		}

		$pos = count($this->positions);

		$this->positions[$pos] = $position;
		$this->widgets[$pos] = $widget;
	}

	public function setSpacing($spacing)
	{
		$this->spacing = $spacing;
	}

	function toString()
	{
		// Calculate the size of table
		$width = 1;
		$height = 1;
		for ($i = 0; $i < count($this->positions); $i++)
		{
			if (($this->positions[$i] == DockStyle::TOP) || ($this->positions[$i] == DockStyle::BOTTOM))
			{
				$height++;
				continue;
			}

			if (($this->positions[$i] == DockStyle::LEFT) || ($this->positions[$i] == DockStyle::RIGHT))
			{
				$width++;
				continue;
			}
		}

		$minx = 0;
		$miny = 0;
		$maxx = $width - 1;
		$maxy = $height - 1;

		$widgetsCalc = array();
		for ($i = 1; $i < count($this->positions); $i++)
		{
			switch ($this->positions[$i])
			{
				case DockStyle::TOP:
					for ($x = $minx; $x <= $maxx; $x++)
						$widgetsCalc[$x][$miny] = $i;

					$miny++;
					break;
				case DockStyle::BOTTOM:
					for ($x = $minx; $x <= $maxx; $x++)
						$widgetsCalc[$x][$maxy] = $i;

					$maxy--;
					break;
				case DockStyle::LEFT:
					for ($y = $miny; $y <= $maxy; $y++)
						$widgetsCalc[$minx][$y] = $i;

					$minx++;
					break;
				case DockStyle::RIGHT:
					for ($y = $miny; $y <= $maxy; $y++)
						$widgetsCalc[$maxx][$y] = $i;

					$maxx--;
					break;
			}
		}

		$widgetsCalc[$minx][$miny] = 0;

		// Draw the table generated
		$str = '<table cellpadding="0" cellspacing="'.$this->spacing.'"';

		if ($this->id != null)
			$str .= ' id="'.$this->id.'"';

		if ($this->getHorizontalAlignment() != null)
			$str .= ' align="'.$this->getHorizontalAlignment().'"';

		if ($this->style != null)
			$str .= ' class="'.$this->style.'"';

		if ($this->getWidth() != null)
			$str .= ' width="'.$this->getWidth().'"';

		$str .= '>';

		$draws = array();

		for ($i = 0; $i < $height; $i++)
		{
			$str .= '<tr>';
			for ($j = 0; $j < $width; $j++)
			{
				$widgetPos = $widgetsCalc[$j][$i];

				if (isset($draws[$widgetPos]) && ($draws[$widgetPos] == -1))
					continue;

				$widgetCols = 0;
				$widgetRows = 0;
				for ($k = 0; $k < $width; $k++)
					if ($widgetsCalc[$k][$i] == $widgetPos)
						$widgetCols++;

				for ($k = 0; $k < $height; $k++)
					if ($widgetsCalc[$j][$k] == $widgetPos)
						$widgetRows++;

				$str .= '<td';
				if ($widgetRows > 1)
					$str .= ' rowspan="'.$widgetRows.'"';

				if ($widgetCols > 1)
					$str .= ' colspan="'.$widgetCols.'"';
					
				if ($this->widgets[$widgetPos]->getHorizontalAlignment() != null)
					$str .= ' align="'.$this->widgets[$widgetPos]->getHorizontalAlignment().'"';

				if ($this->widgets[$widgetPos]->getVerticalAlignment() != null)
					$str .= ' valign="'.$this->widgets[$widgetPos]->getVerticalAlignment().'"';

				if ($this->widgets[$widgetPos]->getWidth() != null)
					$str .= ' width="'.$this->widgets[$widgetPos]->getWidth().'"';
					
				$str .= '>';

				$str .= $this->widgets[$widgetPos]->toString();

				$str .= '</td>';

				$draws[$widgetPos] = -1;
			}

			$str .= '</tr>';
		}

		$str .= '</table>';

		return $str;
	}
}

class DockStyle
{
	const TOP = 1;
	const BOTTOM = 2;
	const LEFT = 3;
	const RIGHT = 4;
	const FILL = 5;
}
?>